/**   
* @Title: DynamicDataSourceContextHolder.java
* @Package org.xaoyaoyao.cloud.dynamic.source
* @Description: TODO
* @author xaoyaoyao
* @date Jul 3, 2019 3:27:43 PM
* @version V1.0   
*/
package org.xaoyaoyao.cloud.dynamic.source;

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: DynamicDataSourceContextHolder
 * @Description: 数据源上下文
 * @author xaoyaoyao
 * @date Jul 3, 2019 3:27:43 PM
 * 
 */
@Slf4j
public class DynamicDataSourceContextHolder {

	/**
	 * 存储已经注册的数据源的key
	 */
	public static List<String> dataSourceIds = new ArrayList<>();

	/**
	 * 线程级别的私有变量
	 */
	private static final ThreadLocal<String> HOLDER = new ThreadLocal<>();

	public static String getDataSourceRouterKey() {
		return HOLDER.get();
	}

	public static void setDataSourceRouterKey(String dataSourceRouterKey) {
		if (log.isInfoEnabled()) {
			log.info(">>>>>>>> 切换至{}数据源", dataSourceRouterKey);
		}
		HOLDER.set(dataSourceRouterKey);
	}

	/**
	 * @Title: removeDataSourceRouterKey
	 * @Description: 设置数据源之前一定要先移除
	 * @return void
	 */
	public static void removeDataSourceRouterKey() {
		HOLDER.remove();
	}

	/**
	 * @Title: containsDataSource
	 * @Description: 判断指定DataSrouce当前是否存在
	 * @param dataSourceId
	 * @return boolean
	 */
	public static boolean containsDataSource(String dataSourceId) {
		return dataSourceIds.contains(dataSourceId);
	}
}
